home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / CMN / string.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  5.8 KB  |  180 lines

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. //string.js
  5. //
  6. //Generic set of functions for manipulating and parsing text strings
  7. //
  8. //------------------------------------------------------------------
  9. //
  10. //
  11. //extractArgs(behFnCallStr){
  12. //escQuotes(theStr){
  13. //unescQuotes(theStr){
  14. //errMsg() {
  15. //badChars(theStr){
  16. //getParam(tagStr,param){
  17. //quote(textStr,quoteType){
  18. //stripSpaces(theStr) {
  19.  
  20. //Given a function call, extracts the args and returns them in array
  21.  
  22. //Respects ', skips over stuff between quotes, and returns them dequoted.
  23. //IMPORTANT: argArray[0] is the function call!! Actual args start at argArray[1].
  24.  
  25. function extractArgs(behFnCallStr){
  26.   var i, theStr, lastPos, argArray;
  27.   argArray = getTokens(behFnCallStr,"(),");
  28.   for (i=0; i<argArray.length; i++) {
  29.     theStr = stripSpaces(unescQuotes(argArray[i]));
  30.     lastPos = theStr.length-1;
  31.     if (theStr.charAt(0) == "'" && lastPos > 0 && theStr.charAt(lastPos) == "'")
  32.       argArray[i] = theStr.substring(1,lastPos);
  33.   }
  34.   return argArray
  35. }
  36.  
  37.  
  38.  
  39. //Passed a string, finds special chars '"\ and escapes them with \
  40.  
  41. function escQuotes(theStr){
  42.   var i, theChar, escStr = "";
  43.   for(var i=0; i<theStr.length; i++) {
  44.     theChar = theStr.charAt(i);
  45.     escStr += (theChar=='"' || theChar=="'" || theChar=="\\")?("\\"+theChar):theChar;
  46.   }
  47.   return escStr;
  48. }
  49.  
  50.  
  51. //Passed a string, finds any escape chars \ and removes them
  52.  
  53. function unescQuotes(theStr){
  54.   var strLen, i, theChar, unescStr = "";
  55.   strLen = theStr.length;
  56.   for(i=0; i<strLen; i++) {
  57.     theChar = theStr.charAt(i);
  58.     if (theChar == "\\" && i < strLen - 1) //if escape char and not end
  59.       theChar = theStr.charAt(++i); //append next char and skip over
  60.     unescStr += theChar;
  61.   }
  62.   return unescStr;
  63. }
  64.  
  65.  
  66. //Emulates printf("blah blah %s blah %s",str1,str2)
  67. //Used for concatenating error message for easier localization.
  68. //Returns assembled string.
  69.  
  70. function errMsg() {
  71. var i,numArgs,errStr="",argNum=0,startPos;
  72.  
  73.   numArgs = errMsg.arguments.length;
  74.   if (numArgs) {
  75.     theStr = errMsg.arguments[argNum++];
  76.     startPos = 0;  endPos = theStr.indexOf("%s",startPos);
  77.     if (endPos == -1) endPos = theStr.length;
  78.     while (startPos < theStr.length) {
  79.       errStr += theStr.substring(startPos,endPos);
  80.       if (argNum < numArgs) errStr += errMsg.arguments[argNum++];
  81.       startPos = endPos+2;  endPos = theStr.indexOf("%s",startPos);
  82.       if (endPos == -1) endPos = theStr.length;
  83.     }
  84.     if (!errStr) errStr = errMsg.arguments[0];
  85.   }
  86.   return errStr;
  87. }
  88.  
  89.  
  90. //Passed a string, finds removes special chars '"! and space
  91.  
  92. function badChars(theStr){
  93.   var i,theChar,isBad=false;
  94.   var someBadChars = " ~!@#$%^&*()_+|`-=\\{}[]:\";'<>,./?";
  95.   for (i=0; i<theStr.length; i++) {
  96.     theChar = theStr.charAt(i);
  97.     if (someBadChars.indexOf(theChar) != -1) isBad = true;
  98.   }
  99.   return isBad;
  100. }
  101.  
  102.  
  103. //Custom non-Javascript code to extract tags and get object names.
  104. //Passed HTML tag (ie IMG), gets the current doc source
  105. //HTML and returns an array of names (empty if unnamed).
  106. //This argument is not case sensitive (can be LAYER, Layer, or layer).
  107. //For Example, given <IMG NAME="myPhoto"> <IMG><IMG name="bobsPhoto">
  108. //returns array: myPhoto,,bobsPhoto
  109.  
  110. function getParam(tagStr,param){
  111.   var j,tokenString;
  112.   var theName = "";
  113.   var tokenArray = new Array;
  114.   tokenArray = getTokens(tagStr," =<>");
  115.   for (j=0; j<(tokenArray.length - 1); j++) {
  116.     tokenString = tokenArray[j].toUpperCase(); //force UPPER CASE
  117.     if (tokenString.indexOf(param.toUpperCase()) == 0) {  //found name
  118.       theName = tokenArray[j+1];  //should return single quoted element in array
  119.       firstChar = theName.charAt(0);
  120.       lastChar = theName.charAt(theName.length - 1);
  121.       if ((firstChar == lastChar) && (firstChar == "'" || firstChar == "\""))
  122.         theName = theName.substring(1,theName.length - 1);
  123.       break;
  124.   } }
  125.   return theName;
  126. }
  127.  
  128. //function: quote
  129. //description: wraps text string in single or double quotes
  130. //argument - textStr
  131. //           quote type - use 1 for single quotes and 2 for double quotes
  132.  
  133. function quote(textStr,quoteType){
  134.    var quote = (quoteType == 1)?"'":'"';
  135.    return quote + textStr + quote;
  136. }
  137.  
  138.  
  139. //Removes any spaces at the beginning or end of the string
  140.  
  141. function stripSpaces(theStr) {
  142.   if (!theStr) theStr = "";  //ensure its not null
  143.   theStr = theStr.replace(/^\s*/,""); //strip leading
  144.   theStr = theStr.replace(/\s*$/,""); //strip trailing
  145.   return theStr;
  146. }
  147.  
  148.  
  149. //Given an object reference string, returns the object name. For ex:
  150. //  objName = getNameFromRef("document.image1"); //returns "image1"
  151. //  objName = getNameFromRef("document.layers['image1']"); //returns "image1"
  152. //
  153. //If given an object in a frame, returns the objName?frameNameOrNum:
  154. //  objName = getNameFromRef("parent.frames['main'].document.image1"); //returns "image1?main"
  155. //This is an expected value for MM_findObj().
  156.  
  157. function getNameFromRef(objRefStr) {
  158.   var c, startPos, objName=objRefStr, frameSearch;
  159.   var lastDot = objRefStr.lastIndexOf(".");
  160.   var lastBracket = objRefStr.lastIndexOf("]");
  161.   
  162.   if (lastDot != -1 || lastBracket != -1) {
  163.     if (lastDot > lastBracket) { //name after a dot
  164.       objName = objRefStr.substring(lastDot+1);
  165.     } else {                     //name in brackets
  166.       while (lastBracket > 0 && ((c=objRefStr.charAt(lastBracket-1))=="'" || c=='"' || c=="\\"))
  167.         lastBracket--;           //skip ',",\
  168.       startPos = lastBracket-1;  //start at end of name
  169.       while (startPos > 0 && ((c=objRefStr.charAt(startPos))!="'" && c!='"' && c!="\\" && c!="["))
  170.         startPos--;              //seek ',",\,[
  171.       objName = objRefStr.substring(startPos+1,lastBracket);
  172.     }
  173.     frameSearch = objRefStr.match(/\.frames\[\\?['"]?([^'"\\]+)\\?['"]?\]/);   //find .frames['foo'] or .frames[3]
  174.     if (frameSearch && frameSearch[1]) { //if framename, add after a question mark
  175.       objName += "?"+frameSearch[1];
  176.     }
  177.   }
  178.   return objName;
  179. }
  180.